//
//  ChooseProMarkVC.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2024/11/5.
//  Copyright © 2024 zwb. All rights reserved.
//

import UIKit
class ChooseProMarkVC: ZyaChooseProductVC {
 
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.mj_header = KSRefreshHeader {
            self.page = 1
            self.network()
        }
   
        self.tableView.emptyDataSetSource = self
        self.tableView.emptyDataSetDelegate = self
        self.tableView.mj_header?.beginRefreshing()
    }
    override func network() {
       var api = API.pFoodQueryGoodsByMark(page: "\(page)", size: "100", s_title: s_title, s_org_id: s_orgid, s_client_id: clientid)
        KSNetwork.shared.doRequest(api,showHud: false) { (data:BaseResult<ZyaFOGoodTModel>) in
            if data.flag == TRUE {
                if let infos = data.infoArrCombine {
                    let newInfos = infos.compactMap { one -> ZyaFOGoodTModel in
                        var nn = one
                        nn.sMark = "1"
                         return nn
                    }
                    if self.page == 1 {
                        self.markSearchArray.removeAll()
                    }
                     self.markSearchArray += newInfos
                    self.tableView.reloadData()
              
                }
            } else {
                KSToast(data.errorCombine.unwrap)
            }
          
            self.tableView.mj_header?.endRefreshing()
        }
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
           guard let cell = tableView.dequeueReusableCell(withIdentifier: ZyaChooseProCell.identifier, for: indexPath) as? ZyaChooseProCell else {
               fatalError()
           }
           
        cell.setLastNodeType(with: self.markSearchArray[indexPath.row])

           
            
           
      // 点击收藏 事件
           cell.markClosure = { [indexPath] in
             
                 
                   self.setMarkNetwork(id: self.markSearchArray[indexPath.row].sGoodsId.unwrap, mark: "0")
                   self.markSearchArray.remove(at: indexPath.row)
                   self.tableView.reloadData()
               
           }
        
           return cell
       }
    override  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   
           return self.markSearchArray.count
     
      
   }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
      
            self.markSearchArray[indexPath.row].selectX =  !self.markSearchArray[indexPath.row].selectX
            self.tableView.reloadData()
    
        
      
        
        
    }
}
